/**
 * Created by asus on 2018/4/7.
 */
/*模块的引入*/
require("./nav.css");
var _mm = require("util/mm.js");
var _user = require("service/user-service.js");
var _cart = require("service/cart-service.js");
var templateIndex = require("./nav.string");

/*定义函数*/
var nav = {
    userData : {
        userName : "",
        role : ""
    },
    init : function(){
        this.loadUsrInfo();
        this.bindEvent();
        return this;
    },
    //1.页面初始化函数
    loadUsrInfo : function(){//1.1加载当前用户信息
        var _this = this;
        _user.checkLogin(
            function(res){
                _this.userData.userName = res.data.username;
                _this.userData.role = res.data.role;
                console.log( _this.userData.role)
                $(".user.not-login").hide().siblings(".user.login").
                show().find(".username").text(res.data.username);
                _this.loadUserImage(res.data.userImg);
                _this.loadCartCount();
            }
            ,
            function(errMsg){
            }
        );
    },

    loadCartCount : function(){//2.2加载购物车数量
        _cart.getCartCount(
            function(res){
                $(".nav .cart-count").text(res.data||0);
            },
            function(errMsg){
                $(".nav .cart-count").text(0);
            }
        );
    },
    loadUserImage : function (userImage) {//2.3加载用户头像
        var navHtml = _mm.renderHtml(templateIndex,{
            userImg : userImage
        });
        $("#nav-item-image").html(navHtml);
    },
    //2.事件绑定函数
    bindEvent : function(){
        //登录点击事件
        $(".js-login").click(function(){
            _mm.doLogin();
        });
        //注册点击事件
        $(".js-register").click(function(){
            window.location.href = "./user-register.html"
        });
        //退出事件
        $(".js-logout").click(function(){
            if(window.confirm("确定退出登录吗？")){
                _user.logout(
                    function(res){
                        window.location.reload();
                    },
                    function(errMsg){
                        _mm.errorTips(errMsg);
                    }
                );
            }else{
                return;
            }
        });
        //我的商城跳转
        this.onclickCampus();
        //我的订单跳转
        this.onclickOrder();
        //我的个人中心跳转
        this.onclickUserCenter();
        //购物车跳转
        this.onclickCart();

    },
    onclickCampus : function () {
        var _this = this;
        $(document).on("click","#nav-campus", function () {
            console.log("ddd",_this.userData.role)
            if(_this.userData.role == 2){
               window.location.href = "./seller-product-list.html";
            }else{
                _mm.successTips("暂未注册为店家 无法查看你的个人商城");
                if(window.confirm("是否马上去注册成店家")){
                    window.location.href = "./seller-register.html";
                }
            }

        });
    },
    onclickOrder : function () {
        var _this = this;
        $(document).on("click","#nav-order", function () {
            if(_this.userData.userName){
                window.location.href = "./order-list.html"
            }else{
                _mm.successTips("暂未登陆 无法查看你的订单信息");
                _mm.doLogin();
            }
        });
    },
    onclickUserCenter : function () {
        var _this = this;
        $(document).on("click","#nav-user-center", function () {
            if(_this.userData.userName){
                window.location.href = "./user-center.html"
            }else{
                _mm.successTips("暂未登陆 无法查看你的个人中心");
                _mm.doLogin();
            }
        });
    },
    onclickCart : function () {
        var _this = this;
        $(document).on("click","#nav-cart", function () {
            if(_this.userData.userName){
                window.location.href = "./cart.html"
            }else{
                _mm.successTips("暂未登陆 无法查看你的购物车");
                _mm.doLogin();
            }
        });
    }

};

module.exports = nav.init();
